package rdd.sparksql;

import org.apache.spark.SparkConf;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

public class SparkSQL104_Sqloperate {
    public static void main(String[] args) {
        final SparkSession sparkSession = SparkSession.builder()
                .master("local[*]")
                .appName("SparkSQL")
                .getOrCreate();

        final Dataset<Row> ds = sparkSession.read().json("data/user.json");

        //因为json是结构化的数据，所以可以将变为表或者视图，便于后续的sql使用
        //参数为表或者视图的名称
        ds.createOrReplaceTempView("user");
        String sql = "select * from user";
        String sql1 = "select avg(age) from user";
        final Dataset<Row> sqlDS = sparkSession.sql(sql);
        Dataset<Row> sqlDS1 = sparkSession.sql(sql1);
        //show方法用于展示操作后的数据
        sqlDS.show();
        sqlDS1.show();
        sparkSession.close();
    }
}
